/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GaoBo
 * Date: 2021-05-11
 * Time: 21:25
 */

class Node {
    public int val;
    public Node prev;
    public Node next;
    public Node(int val) {
        this.val = val;
    }
}

public class TestLinkedList {
    public Node head;
    public Node last;

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(head == null) {
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    public Node searchIndex(int index) {
        Node cur = this.head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if(index < 0 || index > size()) {
            System.out.println("index位置不合法！");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        Node cur = searchIndex(index);
        Node node = new Node(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node cur = this.head;
        while (cur != null ) {
            if(cur.val == key) {
                //说明cur所值的节点 就是要删除的节点
                if(cur == this.head) {
                    this.head = this.head.next;
                    this.head.prev = null;
                }else {
                    //要么是中间位置   要么是尾巴节点
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        //删除尾巴节点
                        last = cur.prev;
                    }
                }
                return;//说明删除完成了
            }else {
                cur = cur.next;
            }
        }
    }



    //删除所有值为key的节点
    public void removeAllKey(int key) {
        Node cur = this.head;
        while (cur != null ) {
            if(cur.val == key) {
                //说明cur所值的节点 就是要删除的节点
                if(cur == this.head) {
                    this.head = this.head.next;
                    //假设全部 是 要删除的数字
                    if(this.head!=null) {
                        this.head.prev = null;
                    }else {
                        last = null;//防止  链表的节点不能被回收
                    }
                }else {
                    //要么是中间位置   要么是尾巴节点
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        //删除尾巴节点
                        last = cur.prev;
                    }
                }
                cur = cur.next;
            }else {
                cur = cur.next;
            }
        }
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //JPS   jmap -live:histo 进程号 > 文件
    public void clear() {
        Node cur = this.head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        last = null;
        head = null;
    }

}
